package node

import (
	"encoding/json"
	"errors"
	"lnzw/common/proto/common_gen"
	"lnzw/engine/cluster"
)

type UrlNode struct {
	cluster.DefaultNode
}

type UrlNodeData struct {
	cluster.DefaultNodeData
}

func (n *UrlNode) NodeType() int32 {
	return int32(common_gen.ServerType_url)
}

type GateNode struct {
	cluster.DefaultNode
}

type GateNodeData struct {
	cluster.DefaultNodeData
	ConnectNum int32  //当前连接数
	Addr       string //对外地址
}

func (n *GateNode) NodeType() int32 {
	return int32(common_gen.ServerType_gate)
}

func (n *GateNode) UnMarshal() error {
	if n.DataRaw == nil {
		return errors.New("DataRaw is empty")
	}
	nData := new(GateNodeData)
	err := json.Unmarshal(n.DataRaw, nData)
	if err != nil {
		return err
	}
	n.Data = nData
	return err
}

type GameMgrNode struct {
	cluster.DefaultNode
}

type GameMgrNodeData struct {
	cluster.DefaultNodeData
}

func (n *GameMgrNode) NodeType() int32 {
	return int32(common_gen.ServerType_gamemgr)
}

type GameNode struct {
	cluster.DefaultNode
}

type GameNodeData struct {
	cluster.DefaultNodeData
	CurrenctUserNum int32
}

func (n *GameNode) NodeType() int32 {
	return int32(common_gen.ServerType_game)
}

func init() {
	cluster.RegisterNodeTypeNode(&GateNode{})
	cluster.RegisterNodeTypeNode(&UrlNode{})
	cluster.RegisterNodeTypeNode(&GameMgrNode{})
	cluster.RegisterNodeTypeNode(&GameNode{})
}
